<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 5] 5.6 Other New Features of Java 1.1</TITLE>
<META NAME="author" CONTENT="David Flanagan">
<META NAME="date" CONTENT="Thu Jul 31 15:52:30 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java">
<META NAME="title" CONTENT="Java in a Nutshell">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java in a Nutshell" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch05_05.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 5<br>Inner Classes and Other New Language Features</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch06_01.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JNUT2-CH-5-SECT-6">5.6 Other New Features of Java 1.1</A></h2>

<P CLASS=para>
While the addition of inner classes is by far the most
important and far-reaching change to the Java language in
Java 1.1, there have been several other changes to the language as
well.  They are:

<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem><tt CLASS=literal>final</tt> local variables, method parameters and
<tt CLASS=literal>catch</tt> statement parameters

<P>
<li CLASS=listitem>Instance initializers

<P>
<li CLASS=listitem>"Blank finals"--<tt CLASS=literal>final</tt> variable and field
declarations without initializer expressions

<P>
<li CLASS=listitem>Anonymous arrays

<P>
<li CLASS=listitem>Class literals

<P>
</UL>
<P CLASS=para>
As you can see, the first two items in this list are
language changes that are related to, though not exclusively
used by, the inner class changes.  We covered <tt CLASS=literal>final</tt>
local variables and parameters in our discussion of local classes
above.  And we covered instance initializers in the
discussion of anonymous classes.  The following subsections
discuss the remaining three changes.

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JNUT2-CH-5-SECT-6.1">Blank Finals</A></h3>

<P CLASS=para>
We've already seen that local variables, method parameters,
and exception parameters of <tt CLASS=literal>catch</tt> statements may
be declared <tt CLASS=literal>final</tt>.  A related change is that <tt CLASS=literal>final</tt>
fields do not require initializers.  In Java 1.0, any
<tt CLASS=literal>final</tt> field had to be initialized as part of the
field declaration.  In Java 1.1, this restriction has been
relaxed.  A field or local variable can be declared
<tt CLASS=literal>final</tt> without specifying an intial value as part of
the declaration.  These "blank finals," as they are called,
must have a value assigned to them before they are ever
used, of course.  And, once a value has been assigned to a
blank final, that value can never be changed.  This allows
you, for example, to use an instance initializer or a
constructor to compute a value for a <tt CLASS=literal>final</tt> field.

<P CLASS=para>
Blank finals are particularly useful in defining immutable
data types.  They allow a class to have immutable fields
that are initialized based on run-time arguments to a
constructor.  Once assigned, these fields cannot be
accidentally or maliciously changed.

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JNUT2-CH-5-SECT-6.2">Anonymous Arrays</A></h3>

<P CLASS=para>
In Java 1.0, you can create and initialize an array with
code like the following:

<P CLASS=para>
<DIV CLASS=screen>
<P>
<PRE>
int[] a = {1, 2, 3, 4, 5};
</PRE>
</DIV>

<P CLASS=para>
Unfortunately, this syntax is only allowed in initializer
expressions that follow the declaration of a field or
variable of array type.  That is, you <I CLASS=emphasis>cannot</I> write
code like this:

<P CLASS=para>
<DIV CLASS=screen>
<P>
<PRE>
int[] a;
a = {1, 2, 3, 4, 5};           // Error
int total = sum({1,2,3,4,5});  // Error
</PRE>
</DIV>

<P CLASS=para>
You cannot write code like that in Java 1.1 either, but
you can write code using a similar array initializer syntax.
When you use the 
<tt CLASS=literal>new</tt> operator to create an array, you may omit the
dimension that specifies the number of array elements to
create and instead follow the empty square bracket pair
(<tt CLASS=literal>[]</tt>) with a list of initial values in curly braces.
Such an expression creates an array large enough to hold
all of the elements specified between the braces, and
initializes the array to contain those elements.  The
elements in braces must all be of the type specified
after the <tt CLASS=literal>new</tt> keyword, of course.

<P CLASS=para>
Code that uses anonymous arrays looks like this:

<P CLASS=para>
<DIV CLASS=screen>
<P>
<PRE>
int[] a;
a = new int[] {1, 2, 3, 4, 5};
int total = sum(new int[] {1, 2, 3, 4, 5});
System.out.println(new char[] {'h', 'e', 'l', 'l', 'o'});
</PRE>
</DIV>

<P CLASS=para>
As you can see, this new syntax allows you to create and
initialize arrays without using a variable initializer, or
without even assigning the array to a variable at all.
That is why arrays created and initialized this way are
called <I CLASS=emphasis>anonymous arrays</I>.

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JNUT2-CH-5-SECT-6.3">Class Literals</A></h3>

<P CLASS=para>
Another major change in Java 1.1 is the introduction of the
Reflection API in the <tt CLASS=literal>java.lang.reflect</tt> package.  As
part of this new package, the <tt CLASS=literal>java.lang.Class</tt> class
has been broadened to represent not just Java classes, but
all Java data types. In other words, there are now special
<tt CLASS=literal>Class</tt> objects that represent each of the
Java primitive types.  

<P CLASS=para>
You can access these special
<tt CLASS=literal>Class</tt> objects through the <tt CLASS=literal>TYPE</tt> field of each
of the primitive wrapper classes.  For example, the static
variable <tt CLASS=literal>Boolean.TYPE</tt> holds the <tt CLASS=literal>Class</tt> object that
represents the <tt CLASS=literal>boolean</tt> data type.  And the
<tt CLASS=literal>Float.TYPE</tt> static variable holds the <tt CLASS=literal>Class</tt>
object that represents the <tt CLASS=literal>float</tt> data type.  A new class
<tt CLASS=literal>Void</tt> has been added, and <tt CLASS=literal>Void.TYPE</tt>
represents the type <tt CLASS=literal>void</tt>.

<P CLASS=para>
The changes described in the paragraph above are all changes
to the Java class libraries, rather than changes to the Java
language itself.  The language change is a related one,
however.  In Java 1.1, you can obtain the <tt CLASS=literal>Class</tt>
object for any class or primitive type by following the
class name or type name by a period and the <tt CLASS=literal>class</tt>
keyword.  For example, <tt CLASS=literal>String.class</tt> evaluates to the
<tt CLASS=literal>Class</tt> object that represents the <tt CLASS=literal>java.lang.String</tt>
class.  Similarly, <tt CLASS=literal>int.class</tt> evaluates to the
special class object <tt CLASS=literal>Integer.TYPE</tt> that represents
the <tt CLASS=literal>int</tt> data type.

<P CLASS=para>
In Java 1.0, it is much more cumbersome (and less
efficient) to obtain a <tt CLASS=literal>Class</tt> object--you have to use the
static <tt CLASS=literal>Class.forName()</tt> method, so you end up with
expressions like:

<P CLASS=para>
<DIV CLASS=screen>
<P>
<PRE>
Class c = Class.forName("java.util.Vector");
</PRE>
</DIV>

<P CLASS=para>
Where in Java 1.1 you can simply write:

<P CLASS=para>
<DIV CLASS=screen>
<P>
<PRE>
Class c = java.util.Vector.class;
</PRE>
</DIV>

<P CLASS=para>
Remember that <tt CLASS=literal>class</tt> is a keyword in Java, so this
syntax does not simply constitute a reference to a static
variable pre-defined in each class.

<P CLASS=para>
This new syntax is meant to simplify use of the new
reflection facilities in Java 1.1.  It is also necessary
because using <tt CLASS=literal>Class.forName()</tt> with inner classes
requires knowledge of the way the compiler transforms the
names of inner classes (i.e., where it replaces "<tt CLASS=literal>.</tt>"
with "<tt CLASS=literal>$</tt>").  While compiler writers need to know
about these transformation rules, Java programmers should
not.  Thus the new <tt CLASS=literal>.class</tt> syntax provides a way to
obtain a <tt CLASS=literal>Class</tt> object that works with inner classes,
as well as with top-level classes and interfaces.

</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch05_05.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch06_01.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Anonymous Classes</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Applets</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
